const { ipcMain } = require('electron');
const { USD_XAU } = require("../../data/gold_price/curl");

// I don't know why electron use webContens to emit event to the ipcRender
module.exports.price = (webContents) => {
    // Use Proxy to Decoupling
    const UsdXauPrice = new Proxy(new USD_XAU(2000), {
        set(target, propKey, value, receiver) {
            if (propKey === 'price') {
                // console.log('set price', target, propKey, value, receiver);

                // emit price-update event
                webContents.send('price-update', value);
            }
            return Reflect.set(target, propKey, value, receiver);
        }
    });

    ipcMain.on('price-start', () => {
        UsdXauPrice.start();
    });

    ipcMain.on('price-stop', () => {
        UsdXauPrice.stop();
    });
}


